﻿using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Maticsoft.DBUtility;
using System.Data;
using Xiaov.SmartWeb.Application.Models;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem
{
  public partial class Compute : BasePage
  {
    private string tableName = "", dwbm = "", nf = "", yf = "", xml = "";
    //string lcbh = "", cjid = "";
    //string lcbh_cjid = "";
    XMLVERSION modelXml = null;
    System.Text.StringBuilder zbbmSB = new System.Text.StringBuilder();
    System.Text.StringBuilder zdSB = new System.Text.StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
      string jsonData = "";
      if (Request.Form["tn"] != null || Request.Form["dwbm"] != null ||
        Request.Form["nf"] != null || Request.Form["yf"] != null)
      {
        tableName = Request.Form["tn"].ToString();
        dwbm = Request.Form["dwbm"].ToString();
        nf = Request.Form["nf"].ToString();
        yf = Request.Form["yf"].ToString();
        //lcbh_cjid = Request.Form["lcbh"].ToString();
        //lcbh = lcbh_cjid.Split(',')[0];
        //cjid = lcbh_cjid.Split(',')[1];
        xml = Server.UrlDecode(Request.Form["xml"].ToString());
        //InitVersion();
        modelXml = new XMLVERSION(nf, yf, dwbm, tableName);
        if (LoadProc("p_compute_sum"))
        {
          //测试： pengwei 保存成功 调用日志来进行保存  
          //string InsertLogSql =  LSSJ.SaveLSSJ(xml,dwbm, nf, yf, tableName, "");

          jsonData = "{\"save\":1}";
        }
        else
        { jsonData = "{\"save\":0}"; }
      }
      else
      {
        jsonData = "{\"save\":0}";
      }
      Response.Write(jsonData);
      Response.ContentType = "text/plain";
      Response.End();

    }

    public bool LoadProc(string ProcdureName)
    {
      if (!string.IsNullOrEmpty(xml))
      {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlNode table = doc.DocumentElement;
        XmlNode modifyRow = table.SelectSingleNode("modifiedRow");
        foreach (XmlNode row in modifyRow.ChildNodes)
        {
          string zbbm = row.Attributes["key"].Value.Substring(6, 4);
          foreach (XmlNode col in row.ChildNodes)
          {
            string colName = col.Name;
            zdSB.Append(colName + ",");
            zbbmSB.Append(zbbm + ",");
          }
        }
      }
      OracleParameter[] parameters ={
            new OracleParameter("v_bbbm",OracleType.VarChar,20),
　　     new OracleParameter("v_dwbm",OracleType.VarChar,20),
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2),
            new OracleParameter("c_zbbm",OracleType.Clob),
            new OracleParameter("c_zd",OracleType.Clob)
　　      };
      parameters[0].Value = tableName.ToUpper();
      parameters[1].Value = dwbm.ToUpper();
      parameters[2].Value = nf;
      parameters[3].Value = yf;
      parameters[4].Value = string.IsNullOrEmpty(zbbmSB.ToString()) ? "1" : zbbmSB.ToString().Trim(new char[] { ',' });
      parameters[5].Value = string.IsNullOrEmpty(zdSB.ToString()) ? "1" : zdSB.ToString().Trim(new char[] { ',' });
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure(ProcdureName, parameters); odr.Close();
        return true;
      }
      catch
      {
        return false;
      }
    }
  }
}